package cn.stylefeng.guns.modular.service;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.modular.entity.Frame;
import cn.stylefeng.guns.modular.mapper.FrameMapper;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Map;

/**
 *
 * 日程框架服务
 *
 * @author third_e
 * @create 2019/12/10 0010-下午 3:50
 */
@Service
public class FrameService extends ServiceImpl<FrameMapper, Frame> {


    /**
     * 保存日程框架和日程框架项
     * @param frame
     */
    @Transactional(rollbackFor = Exception.class)
    public void addFrame(Frame frame){
        //判断日程框架是否重复
//        Frame theFrame = this.getByName(frame.getFrameName());
//        if(theFrame !=null){
//            throw new ServiceException(BizExceptionEnum.FRAME_DUPLIOATE_NAME);
//        }
        frame.setFrameId(null);
        this.save(frame);
    }

    /**
     * 通过日程框架名获取日程框架id
     */
    public Frame getByName(String frameName) {
        return this.baseMapper.getByFrameName(frameName);
    }

    /**
     * 根据条件查询框架列表
     * 包含用户发布了，未发布，已关闭的框架
     *
     * @return
     */
//    public Collection<Frame> selectFrames(String condition, Long userId){
    public Page<Map<String, Object>> selectFrames(String nameOrcontent, String otherOrmyself, String beginTime, String endTime, Long userId){
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.selectFrames(page, nameOrcontent, otherOrmyself, beginTime, endTime, userId);
    }


    public Frame getAllFrameInfo(Long frameId){

        return this.baseMapper.selectById(frameId);
    }


    /**
     * 保存编辑日程框架
     */

    public void editAdd(Frame frame){
//        Frame oldFrame = this.getById(frame.getFrameId());
        this.updateById(frame);
    }

    /**
     * 发布日程框架
     */
    public  void release(Long frameId){
        //判断日程框架是否重复
        Frame theFrame = this.getById(frameId);
        if(theFrame ==null){
            throw new ServiceException(BizExceptionEnum.NO_THIS_FRAME);
        }
        this.baseMapper.releaseFrame(frameId);
    }

    /**
     * 删除日程框架
     */
    public void deleteFrame(Long frameId){
        //判断日程框架是否存在
        Frame theFrame = this.getById(frameId);
        if(theFrame ==null){
            throw new ServiceException(BizExceptionEnum.NO_THIS_FRAME);
        }
        this.baseMapper.deleteFrame(frameId);
    }

    /**
     * 获取可用日程框架
     *
     */
    public Collection<Frame> selectEnableFrame(Long userId ,String condition){
        return this.baseMapper.selectEnableFrame(userId, condition);
    }
}
